#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2025/10/10
# @Author  : Syaoran
# @File    : application_manage.py
# @Desc    : 应用管理

from infrastructure.po.impl.application_impl import ApplicationImpl
from infrastructure.utils.http import responser
from pprint import pprint, pformat

class ApplicationService(object):

    @classmethod
    def get_application_list(cls, applicationName=None, page=1, page_size=999):
        """
        查询所有应用列表
        :param applicationName: 查询
        :param page: 页码
        :param page_size: 每页数量
        :return:
        """
        response_data = []
        application_list, total = ApplicationImpl.get_application_list(applicationName=applicationName, page=page, page_size=page_size)
        for application in application_list:
            # pprint("application:", application)
            response_data.append(
                {
                    "id": application.id,
                    "applicationName": application.application_name,
                    "status": application.status,
                    "description": application.description,
                    "createTime": application.create_time,
                    "updateTime": application.update_time
                }
            )
        return responser.send(10000, data={"total": total, "data": response_data})
    
    @classmethod
    def update_application(cls, id, application_name, status, description):
        """
        更新应用信息
        :param id: 应用ID
        :param application_name: 应用名称
        :param status: 状态
        :param description: 描述
        :return:
        """
        response_code = ApplicationImpl.update_application(
            id=id,
            application_name=application_name,
            status = status,
            description=description
        )
        if response_code:
            return responser.send(10000)
        else:
            return responser.send(10006)
        
    @classmethod
    def get_application_info(cls, id):
        """
        获取应用信息
        :param id: 应用ID
        """
        application = ApplicationImpl.get_application_info(id=id)
        data = {
            "id": application.id,
            "applicationName": application.application_name,
            "status": application.status,
            "description": application.description,
            "version": application.version,
            "createTime": application.create_time,
            "updateTime": application.update_time
        }
        return responser.send(10000, data={"data": data})